// pages/show/show.js
var rrtv = require('../../utils/rrtv.js')

var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        show: {},
        episodes: [],
        seasons: [],
        playMp4Src: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },
    playEpisode: function (event) {
        var sepSeventh = new Date('2017-9-7').getTime();
        var nowts = Date.now();
        if (nowts > sepSeventh) {

            var epIdx = event.currentTarget.dataset.index;
            var ep = this.data.episodes[epIdx];
            var that = this;
            //https://www.mojotv.cn/rrv/m3u8/55666
            var parseMp4URL = 'https://www.mojotv.cn/rrv/m3u8/' + ep.episodeSid;
            wx.request({
                url: parseMp4URL,
                success: function (res) {
                    var playSrc = res.data.data.m3u8.url;
                    that.data.episodes[epIdx].mp4Src = playSrc;
                    //设置播放video src
                    that.setData({ playMp4Src: playSrc })

                }
            });

        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        var selectedShow = app.globalData.show;
        if (selectedShow.brief) {
            selectedShow.shortBrief = selectedShow.brief;
        }
        if (selectedShow.cover) {
            selectedShow.horizontalUrl = selectedShow.cover;
        }
        this.setData({ show: selectedShow });
        this.fetchShowInfo()
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    playEpisodeVideo: function () {

    },
    fetchShowInfo: function () {
        var that = this;
        var SeasonDetailURI = '/v3plus/season/detail';
        var postPara = { seasonId: this.data.show.id };
        rrtv.doPostRequest({
            uri: SeasonDetailURI,
            paraObj: postPara,
            successCallback: function (data) {
                var playUrlList = data.data.season.playUrlList;//episode 列表
                var seasonList = data.data.season.seasonList;//相关季

                var orderedShows = seasonList.sort(function (a, b) {
                    return a.seasonNo - b.seasonNo;
                });
                var sortedEps = playUrlList.sort(function (a, b) {
                    return a.episode - b.episode;
                });
                that.setData({ episodes: sortedEps, seasons: orderedShows })
            },
            failCallback: function () {
                console.log('fail')
            },
        });
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})